package com.facebank.usersupport.controller;

import com.facebank.usersupport.model.Associator;
import com.facebank.usersupport.model.RestModel;
import com.facebank.usersupport.service.IJsonpService;
import com.facebank.usersupport.util.ImgSaveUtil;
import com.facebank.usersupport.util.SessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * @Auther: yaozun
 * @Date: 2018/3/21 0021 15:05
 * @Description:
 */

@RestController
public class JsonpController {
    @Autowired
    private IJsonpService jsonpService;

    //登录
    @RequestMapping(value = "/travel/login", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestModel login(String username, String password, HttpSession session) {
        return jsonpService.login(username, password, session);
    }
    //
    @RequestMapping(value = "/travel/login_url", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestModel loginUrl() {
        return new RestModel("205", "用户没登录");
    }
    //注销
    @RequestMapping(value = "/travel/loginout", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestModel loginout(HttpSession session) {
        if (SessionUtil.getAssociatorId(session) == null || SessionUtil.getAssociatorId(session) == "") {
            return new RestModel("205", "用户没登录");
        }
        SessionUtil.removeAssociatorAttribute(session);
        return new RestModel("注销成功");
    }

    //注册
    @RequestMapping(value = "/travel/register", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestModel register(String phone, String password, String password_confirm, String email) {
        return jsonpService.register(phone, password, password_confirm, email);
    }

    //注册
    @RequestMapping(value = "/travel/change_password", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestModel changePassword(String password, String passwordConfirm, HttpSession session) {
        return jsonpService.changePassword(password, passwordConfirm, session);
    }

    //个人信息
    @RequestMapping(value = "/travel/personal", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestModel personal(HttpSession session) {
        if (SessionUtil.getAssociatorId(session) == null || SessionUtil.getAssociatorId(session) == "") {
            return new RestModel("205", "用户没登录");
        }
        Long assId = Long.valueOf(SessionUtil.getAssociatorId(session));
        return jsonpService.personal(assId);
    }

    //修改个人信息
    @RequestMapping(value = "/travel/change_personal", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestModel changePersonal(Associator associator, HttpSession session) {
        if (SessionUtil.getAssociatorId(session) == null || SessionUtil.getAssociatorId(session) == "") {
            return new RestModel("205", "用户没登录");
        }
        Long assId = Long.valueOf(SessionUtil.getAssociatorId(session));
        associator.setAssociatorId(assId);
        return jsonpService.changePersonal(associator);
    }

    //活动展示
    @RequestMapping(value = "/travel/activity_show", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestModel activityShow(Short route_type) {
        return jsonpService.activityShow(route_type);
    }

    //活动详情
    @RequestMapping(value = "/travel/activity_detail", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestModel activityDetail(Integer id) {

        return jsonpService.detail(id);
    }

    //上传头像
    @RequestMapping(value = "/travel/head_img", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestModel headImg(Integer id) {
        return jsonpService.detail(id);
    }

    //消息显示
    @RequestMapping(value = "/travel/message", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestModel message(){
        return jsonpService.message();
    }
    //展示首頁活動
    @RequestMapping(value = "/travel/activity_list", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestModel activityList( ) {
        return jsonpService.activityList();
    }

    //推荐列表
    @RequestMapping(value = "/travel/guess_activity_list", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestModel guessActivityList(HttpSession session) {
        String assId= SessionUtil.getAssociatorId(session);
        return jsonpService.guessActivityList(assId);
    }

    //活动评价显示
    @RequestMapping(value = "/travel/activity_evaluate", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestModel activityEvaluate(Long id) {
        return jsonpService.showEvaluation(id);
    }

    //修改头像
    @RequestMapping(value = "/travel/change_imag", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestModel change_imag(String base64 ,HttpSession session) {
        System.out.println("宝贝"+base64);
        String fileName = ImgSaveUtil.base64Tofile(base64);
//        Long assId = Long.valueOf(SessionUtil.getAssociatorId(session));
        Long assId = 123456789L;
        Associator associator = new Associator();
        associator.setHeadImg(fileName);
        associator.setAssociatorId(assId);
        return jsonpService.changePersonal(associator);
    }

}